// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import ElementUI from 'element-ui'
// 引用element ui自定义主题
import '../theme/index.css'
// import 'element-ui/lib/theme-chalk/index.css'
// import 'jquery'
import flexible from './utils/flexible'
import store from './store'
import router from './router'
import common from './utils/common'
import filters from './utils/filters'
import axios from './utils/request'
import route from './utils/route'
import App from './App'
import './style/reset.less'
import './style/style.less'
import './style/theme.less'
/*
* 引用echarts
* 单独引入bar line scatter pie四种基础图表
*/
let echarts = require('echarts/lib/echarts') // 引入ECharts主模块
require('echarts/lib/chart/bar') // 引入柱状图
require('echarts/lib/chart/line') // 引入折线图
require('echarts/lib/chart/scatter') // 引入散点图
require('echarts/lib/chart/pie') // 引入饼状图
require('echarts/lib/component/tooltip') // 引入提示框
require('echarts/lib/component/title') // 引入标题组件

Vue.config.productionTip = false
Vue.use(ElementUI)
Vue.prototype.$http = axios
Vue.prototype.$method = common
Vue.prototype.$filter = filters
Vue.prototype.$routeNav = route
Vue.prototype.$echarts = echarts

// 全局引入过滤器
Object.keys(filters).forEach(key => {
    Vue.filter(key, filters[key])
})
// 页面自适应
flexible.remDesign(1920, 1920)

router.beforeEach((to, from, next) => {
    setTimeout(() => {
        if (!store.getters.user && !store.getters.routerChange) {
            // 请求登录信息
            store.dispatch('loginBySession').then(res => {
                // 动态添加可访问路由表
                store.dispatch('generateAllRouters', res).then(routers => {
                    router.options.routes.push(...routers)
                    router.addRoutes(routers)
                    // 标记路由已动态更改
                    store.dispatch('updateRouters')
                    routeRedirect(next, store.getters.user, to, true)
                })
            }).catch(() => {
                // 登录信息请求失败时动态添加所有路由
                store.dispatch('generateAllRouters').then(routers => {
                    router.options.routes.push(...routers)
                    router.addRoutes(routers)
                    // 标记路由已动态更改
                    store.dispatch('updateRouters')
                    routeRedirect(next, store.getters.user, to, true)
                })
            })
        } else {
            routeRedirect(next, store.getters.user, to)
        }
    }, 0)

    function routeRedirect(next, user, to, replace) {
        let obj = router.options.routes.find((item) => {
            return item.path === ('/' + to.path.split('/')[1])
        })
        if (!window.routeAuth) window.routeAuth = store.getters.user && store.getters.user.authTree
        if (!window.routerFirst) window.routerFirst = route.jumpFirstRouter(window.routeAuth)
        if (obj && obj.path && to.path !== '/') {
            let hasAuth = route.getRouter(to)
            if (hasAuth || to.path.indexOf('401') > -1) {
                if (replace) {
                    next({...to, replace: true})
                } else {
                    next(...to)
                }
            } else {
                next({path: '/401', replace: true})
            }
            // else if (window.routerFirst) {
            //         next({name: window.routerFirst.name, replace: true})
            //     }
        } else {
            if (window.routerFirst) {
                next({name: window.routerFirst.name, replace: true})
            } else {
                next({path: '/401', replace: true})
            }
            // if (!user || !user.bmanager) {
            //     next({path: '/my/index', replace: true})
            // } else if (user.bmanager === 1) {
            //     next({path: '/manager/index', replace: true})
            // } else {
            //     next({path: '/sp-manager/advisory', replace: true})
            // }
        }
    }
})

// 路由跳转后更新主题显示区域，刷新滚动条
router.afterEach((to, from) => {
    common.updateView()
    window.$history++
})

/* eslint-disable no-new */
window.$vm = new Vue({
    el: '#app',
    router,
    store,
    components: {App},
    template: '<App/>'
})

window.$history = 0
